package tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import javax.activity.InvalidActivityException;
import org.junit.Before;
import org.junit.Test;
import code.DateFormat;
import code.Photos;

public class PhotosTest {

	// -------------------------------------------------------------------------

	/**
	 * Setup by cleaning up the previous run
	 * 
	 * @throws InterruptedException
	 * @throws IOException
	 * @throws InvalidActivityException
	 */
	@Before
	public void setUp() throws InvalidActivityException, IOException, InterruptedException {
		TestHelper.cleanDirectory(TestHelper.RootDir);
	}

	// Basic Tests ------------------------------------------------------------

	@Test
	public void testPhotos_DefaultConstructor() {
		Photos photos = new Photos();
		assertNotNull(photos);
		assertEquals(null, photos.getRoot());
	}

	@Test
	public void testPhotos_Constructor() {
		File f1 = new File("C:\\");

		Photos photos = new Photos(f1);
		assertNotNull(photos);
		assertFalse(false);
	}

	@Test(expected = NullPointerException.class)
	public void testPhotos_ConstructorExceptionNull() throws Exception {
		Photos photos = new Photos(null);
		assertNotNull(photos); // should not even get here
	}

	// Method Tests ------------------------------------------------------------

	@Test
	public void testPhotos_SetRoot() throws Exception {
		File f1 = new File("C:\\");

		Photos photos = new Photos();
		assertNotNull(photos);

		photos.setRoot(f1);
		File fActual = photos.getRoot();
		assertEquals(f1, fActual);
	}

	@Test(expected = ExceptionInInitializerError.class)
	public void testPhotos_SetRootInvalid() throws Exception {
		TestHelper.createPhotos(TestHelper.RootDir, TestHelper.JPG, 1);
		File f1 = new File("c:\\test.txt");

		Photos photos = new Photos();
		assertNotNull(photos);

		photos.setRoot(f1);
		File fActual = photos.getRoot();
		assertEquals(f1, fActual);
	}

	@Test
	public void testPhotos_LoadRightNumber() throws Exception {
		int fileCount = 10;
		TestHelper.createPhotos(TestHelper.RootDir, TestHelper.JPG, fileCount);

		Photos photos = new Photos(TestHelper.RootDir);
		int count = photos.loadPhotos();
		assertEquals("Expected number of photos in this directory not found", count, fileCount);
	}

	@Test
	public void testPhotos_LoadRightNumberWithExtraFiles() throws Exception {
		int fileCount = 10;
		TestHelper.createPhotos(TestHelper.RootDir, TestHelper.JPG, fileCount);
		TestHelper.createPhotos(TestHelper.RootDir, TestHelper.TXT, fileCount);

		Photos photos = new Photos(TestHelper.RootDir);
		int count = photos.loadPhotos();
		assertEquals("Expected number of photos in this directory not found (should be 10, not 20)", count, fileCount);
	}

	@Test
	public void testPhotos_LoadDirsDash() throws InvalidActivityException {
		TestHelper.createDateBasedPhotoDirs(TestHelper.RootDir, DateFormat.yyyyMMddDash, 2010, 10, 20, " - description");
		Photos photos = new Photos(TestHelper.RootDir);
		int count = photos.loadPhotos();
		assertEquals("Expected number of photos in this directory not found", count, 0);
	}

	@Test
	public void testPhotos_LoadDirsUnder() throws InvalidActivityException {
		TestHelper.createDateBasedPhotoDirs(TestHelper.RootDir, DateFormat.yyyyMMddUnder, 2010, 10, 20, " - description");
		Photos photos = new Photos(TestHelper.RootDir);
		int count = photos.loadPhotos();
		assertEquals("Expected number of photos in this directory not found", count, 0);
	}

	@Test
	public void testPhotos_CollapseSubAlbums() throws Exception {
		File[] albums = TestHelper.createDateBasedPhotoDirs(TestHelper.RootDir, DateFormat.yyyyMMddUnder, 2010, 1, 3, " - description");

		// create sub albums in each of the albums (6 sub albums in total)
		File[] subAlbum1 = TestHelper.createDateBasedPhotoDirs(albums[0], DateFormat.yyyyMM, 2010, 3, 2);
		File[] subAlbum2 = TestHelper.createDateBasedPhotoDirs(albums[1], DateFormat.yyyyMM, 2010, 4, 2);
		File[] subAlbum3 = TestHelper.createDateBasedPhotoDirs(albums[2], DateFormat.yyyyMM, 2010, 5, 2);

		// in each of the sub albums, create a photo (6 pics in total)
		TestHelper.createFile(subAlbum1[0], "Image_01", 1, "", "jpg");
		TestHelper.createFile(subAlbum1[1], "Image_02", 1, "", "jpg");
		TestHelper.createFile(subAlbum2[0], "Image_03", 1, "", "jpg");
		TestHelper.createFile(subAlbum2[1], "Image_04", 1, "", "jpg");
		TestHelper.createFile(subAlbum3[0], "Image_05", 1, "", "jpg");
		TestHelper.createFile(subAlbum3[1], "Image_06", 1, "", "jpg");

		Photos photos = new Photos(TestHelper.RootDir);
		assertEquals(6, photos.loadPhotos());
		assertEquals("Size should return the same as the load", 6, photos.getRootAlbum().photosCountRecursive());
		assertEquals("Sub albums added were 3 in total", 3, photos.getRootAlbum().albumsCount());

		assertTrue(photos.collapseSubAlbums());
		assertEquals("Should still have the same number of photos", 6, photos.getRootAlbum().photosCountRecursive());
		assertEquals("Collapse should have deleted child albums", 0, photos.getRootAlbum().albumsCount());
	}
}
